package com.qutan.app.core.domain;

import com.alibaba.fastjson.JSON;
import com.i7time.entity.FriendInfo;
import com.qutan.app.core.domain.enumclass.SexType;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pwy on 16/5/15
 * <p>
 * 好友
 */
@Entity
@Table(name = "friend")
@DynamicInsert(value = true)
@DynamicUpdate(value = true)
public class Friend extends IDEntity {

    @Column(columnDefinition = "bigint(11) comment '用户id' ")
    private long userId;

    @Column(columnDefinition = "bigint(11) comment '好友的id '")
    private long friendId;

    @Column(columnDefinition = "varchar (40) comment '给好友取的昵称 '")
    private String friendNickName;

    @Column(columnDefinition = "varchar (80) comment '好友头像' ")
    private String friendAvatar;

    @Column(columnDefinition = "varchar (1000) comment '黑名单' ")
    private String blackListStr;

    @Column(columnDefinition = "int comment '性别0.女,1.男,2.其他' ")
    @Enumerated(EnumType.ORDINAL)
    private SexType sex;

    /**
     * 黑名单列表
     */
    @Transient
    private List<Long> blackList = new ArrayList<>();

    public SexType getSex() {
        return sex;
    }

    public void setSex(SexType sex) {
        this.sex = sex;
    }

    public String getBlackListStr() {
        if (!this.blackList.isEmpty()) {
            return JSON.toJSONString(this.blackList);
        }
        return blackListStr;
    }

    public void setBlackListStr(String blackListStr) {
        this.blackListStr = blackListStr;
        if (StringUtils.isBlank(blackListStr)) return;
        this.blackList = JSON.parseArray(blackListStr, Long.class);
    }

    public List<Long> getBlackList() {
        return blackList;
    }

    public void setBlackList(List<Long> blackList) {
        this.blackList = blackList;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public long getFriendId() {
        return friendId;
    }

    public void setFriendId(long friendId) {
        this.friendId = friendId;
    }

    public String getFriendNickName() {
        return friendNickName;
    }

    public void setFriendNickName(String friendNickName) {
        this.friendNickName = friendNickName;
    }

    public String getFriendAvatar() {
        return friendAvatar;
    }

    public void setFriendAvatar(String friendAvatar) {
        this.friendAvatar = friendAvatar;
    }

    public FriendInfo convert2FriendInfo() {
        FriendInfo friendInfo = new FriendInfo();
        friendInfo.setId(this.id);
        friendInfo.setFriendAvatar(this.friendAvatar);
        friendInfo.setFriendId(this.friendId);
        friendInfo.setUserId(this.userId);
        friendInfo.setFriendNickName(this.friendNickName);
        int sex=this.sex==null?SexType.FEMALE.getSort():this.sex.getSort();
        friendInfo.setSex(String.valueOf(sex));
        return friendInfo;
    }
}
